package com.laiketui.common.mapper;

import com.laiketui.domain.plugin.group.PtGoGroupOrderModel;
import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.core.db.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * @author Trick
 */
public interface PtGoGroupOrderModelMapper extends BaseMapper<PtGoGroupOrderModel> {

    /**
     * 统计拼团商品购买数量
     *
     * @param storeId -
     * @param goodsId -
     * @return Integer
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021-04-26 15:33:11
     */
    @Select("select count(*) as sum " +
            "                   from lkt_pt_go_group_order_details as d " +
            "                   left join lkt_pt_go_group_order as o on d.r_sNo=o.sNo " +
            "                   where d.p_id=#{goodsId} and o.otype='pt' and d.r_status!=0 and d.r_status!=6 and d.store_id=#{storeId}")
    Integer countGroupGoodsPayNum(int storeId, int goodsId) throws LaiKeAPIException;


    /**
     * 修改订单状态
     *
     * @param storeId -
     * @param orderno -
     * @param status  -
     * @return Integer
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/4/1 11:19
     */
    @Update("update lkt_pt_go_group_order set `status`=#{status} where sno=#{orderno} and store_id=#{storeId}")
    Integer updateStatus(int storeId, String orderno, int status) throws LaiKeAPIException;

    /**
     * 获取拼团订单列表
     *
     * @param map -
     * @return List
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/3/24 9:43
     */
    List<Map<String, Object>> getGrouporder(Map<String, Object> map) throws LaiKeAPIException;
    List<Map<String, Object>> getGrouporderList(Map<String, Object> map) throws LaiKeAPIException;

    /**
     * 当前用户成功拼团数量
     *
     * @param storeId -
     * @param userId  -
     * @param ptCode  -
     * @return Integer
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/3/26 18:44
     */
    @Select("select count(id) as count from lkt_pt_go_group_order where ptcode = #{ptCode} and user_id = #{userId} " +
            "and status NOT IN (0,6,7,8) and ptcode != '' and store_id = #{storeId}")
    Integer countGroupPayOrderNum(int storeId, String userId, String ptCode) throws LaiKeAPIException;


    /**
     * 更具拼团编号获取成功拼团的用户信息
     *
     * @param storeId -
     * @param ptcode  -
     * @return List
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/3/24 17:38
     */
    @Select("SELECT i.user_id, u.headimgurl FROM lkt_pt_go_group_order i LEFT JOIN lkt_user u ON i.user_id = u.user_id " +
            "WHERE u.store_id = #{storeId} AND i.ptcode = #{ptcode} AND i.status NOT IN (0, 6, 7) ORDER BY i.id ASC")
    List<Map<String, Object>> getMemberGrouporder(int storeId, String ptcode) throws LaiKeAPIException;


    /**
     * 统计
     *
     * @param map -
     * @return Integer
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/3/25 10:17
     */
    Integer countGrouporder(Map<String, Object> map) throws LaiKeAPIException;
    Integer countGrouporderList(Map<String, Object> map) throws LaiKeAPIException;


    /**
     * 修改订单拼团状态
     *
     * @param storeId -
     * @param ptcode  -
     * @param status  -
     * @return Integer
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/4/2 18:28
     */
    @Update("update lkt_pt_go_group_order set ptstatus=#{status} where ptcode=#{ptcode} and store_id=#{storeId}")
    Integer updatePtStatus(int storeId, String ptcode, int status) throws LaiKeAPIException;

    /**
     * 查询拼团活动已经购买多了多少
     *
     * @param activityId -
     * @return int
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/2/20 14:06
     */
    @Select("select sum(num) as volume from lkt_pt_go_group_order where activity_no = #{activityId}")
    Integer groupSumOrderByNum(int activityId) throws LaiKeAPIException;
}